package org.fu.crowd.controller;

import org.fu.crowd.constant.CrowdConstant;
import org.fu.crowd.po.Member;
import org.fu.crowd.service.api.MemberService;
import org.fu.crowd.utils.Common;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class MemberController {

    @Resource
    private MemberService memberService;


    /**
     *      根据账号查询member
     * @param loginacct
     * @return
     */
    @RequestMapping("/getMember/ByLogin/AcctRemote")
    public Common<Member> getMemberPOByLoginAcctRemote(@RequestParam("loginacct") String loginacct){
        try {
            Member member = memberService.getMemberPOByLoginAcct(loginacct);
            return Common.successWithoutData(member);
        }catch (Exception e){
            return Common.failed(e.getMessage());
        }
    }


    /**
     *      保存注册信息
     */
    @RequestMapping("/saveMember/remote")
    public Common<String> saveMemberRemote(@RequestBody Member member){
        try {
            memberService.saveMember(member);
            return Common.successWithoutData();
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException){
                return Common.failed(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_INUSE);
            }
            return Common.failed(CrowdConstant.REGISTER_FAILED);
        }
    }




}
